﻿using Microsoft.JScript;
using System.Diagnostics.Contracts;
using System.Text;
using System.Text.RegularExpressions;

namespace ZHello.Utils
{
    public class RegexUtil
    {
        public static bool HasChinese(string value)
        {
            Contract.Requires(!string.IsNullOrEmpty(value));
            var parttern = @"[\u4e00-\u9fa5]+";
            parttern = @"[\S\s]*[\u4E00-\u9FFF]+[\S\s]*+";
            var regex = new Regex(parttern, RegexOptions.IgnoreCase);
            var res = regex.IsMatch(value);
            return res;
        }

    }

    public class ChineseUtil
    {
        public static bool HasChinese(string value)
        {
            Contract.Requires(!string.IsNullOrEmpty(value));
            var parttern = @"[\u4e00-\u9fa5]+";
            //parttern = @"[\S\s]*[\u4E00-\u9FFF]+[\S\s]*+";
            var regex = new Regex(parttern, RegexOptions.IgnoreCase);
            var res = regex.IsMatch(value);
            return res;
        }

        public static bool IsChinese(char value)
        {
            var regex = new Regex("^[\u4e00-\u9fa5]$");
            return regex.IsMatch(value.ToString());
        }

        public static string GetAreaCode(string value)
        {
            var arry =Encoding.Default.GetBytes(value);
            var front = (short)(arry[0] - '\0');
            var back = (short)(arry[1] - '\0');
            var result = (front - 160).ToString() + (back - 160).ToString();
            return result;
        }
    }
}